using CharacterCreation.Views.Contracts;
using CharacterCreation.Data.Services;
using CharacterCreation.Data.Models;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using CharacterCreation.Views.ChildWindows;

namespace CharacterCreation.Views {
    /// <summary>
    /// The viewmodel 
    /// </summary>
    public class CharacterNewSpecialsViewModel : BaseViewModel {

        private CharacterService _characterService = CharacterService.CreateInstance();

        /// <summary>
        /// The new character
        /// </summary>
        public Character Character { get { return _characterService.NewItem; } }

        /// <summary>
        /// Gets or sets the SelectedCharacterSpecial
        /// </summary>
        public CharacterSpecial SelectedCharacterSpecial {
            get { return _SelectedCharacterSpecial; }
            set {
                _SelectedCharacterSpecial = value;
                RaisePropertyChanged("SelectedCharacterSpecial");
            }
        }
        private CharacterSpecial _SelectedCharacterSpecial;

        /// <summary>
        /// Add a characterSpecial to a character
        /// </summary>
        public ICommand AddCommand {
            get {
                return new DelegateCommand(() => {
                    var specialService = SpecialService.CreateInstance();
                    specialService.Clear();
                    PopupWindow popupWindow = new PopupWindow(
                        new BaseView[] { new SpecialSelectionListView() }
                    );
                    popupWindow.Closing += (s, e) => {
                        if (popupWindow.DialogResult != null && (bool)popupWindow.DialogResult) {
                            CharacterSpecial characterSpecial = null;
                            this.Character.Specials.Add(characterSpecial = new CharacterSpecial() { Special = specialService.CurrentItem });
                            characterSpecial.CalculateSpecialLevel(this.Character);
                        }
                    };
                    popupWindow.ShowDialog();
                });
            }
        }

        /// <summary>
        /// Remove a character Special
        /// </summary>
        public ICommand DeleteCommand {
            get {
                return new DelegateCommand(
                    () => {
                        if (this.SelectedCharacterSpecial != null)
                            this.Character.Specials.Remove(SelectedCharacterSpecial);
                    });
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public CharacterNewSpecialsViewModel() { }
    }
}
